<template>
  <div class="product-container">
    <!-- 左侧分类栏 -->
    <div class="category-nav">
      <h3>商品分类</h3>
      <ul>
        <li
            v-for="item in categories"
            :key="item.type"
            :class="{ active: activeType === item.type }"
            @click="handleTypeChange(item.type)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>

    <!-- 右侧商品列表 -->
    <div class="product-list">
      <h2>
        {{ currentCategoryName }}
        <span class="update-time">（更新于：{{ updateTime }}）</span>
      </h2>

      <!-- 加载状态 -->
      <div class="loading" v-if="loading">加载中...</div>

      <!-- 错误提示 -->
      <div class="error" v-if="error">
        <p>加载失败，请稍后重试</p>
        <button @click="fetchProducts">重试</button>
      </div>

      <!-- 商品网格 -->
      <div class="product-grid" v-if="!loading && !error">
        <div class="product-item" v-for="product in products" :key="product.id">
          <div class="product-img">📱</div>
          <div class="product-info">
            <h3>{{ product.name }}</h3>
            <p>品牌：{{ product.brand }}</p>
            <p>发布日期：{{ product.releaseDate }}</p>
            <p class="price">¥{{ product.price }}</p>
          </div>
        </div>

        <!-- 空状态 -->
        <div class="empty" v-if="products.length === 0">
          暂无{{ currentCategoryName }}数据
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import request from '../utils/request';

// 分类数据
const categories = [
  { type: 'phone', name: '手机' },
  { type: 'laptop', name: '笔记本' },
  { type: 'accessory', name: '配件' }
];

// 响应式数据
const activeType = ref('phone'); // 当前激活的类型
const products = ref([]); // 商品列表
const loading = ref(false); // 加载状态
const error = ref(false); // 错误状态
const updateTime = ref(''); // 更新时间

// 计算属性：当前分类名称
const currentCategoryName = computed(() => {
  return categories.find(c => c.type === activeType.value)?.name || '';
});

// 切换分类
const handleTypeChange = (type) => {
  activeType.value = type;
  fetchProducts(); // 重新请求数据
};

// 请求商品数据（最新 Vue 3 setup 语法 + 异步/await）
const fetchProducts = async () => {
  try {
    loading.value = true;
    error.value = false;
    products.value = [];

    // 仅手机类型有接口，其他类型模拟空数据
    if (activeType.value === 'phone') {
      const res = await request.get('/products', {
        params: { type: activeType.value } // 传递类型参数
      });
      if (res.code === 200) {
        products.value = res.data;
      }
    }

    // 更新时间
    updateTime.value = new Date().toLocaleString();
  } catch (err) {
    console.error('请求失败：', err);
    error.value = true;
  } finally {
    loading.value = false;
  }
};

// 组件挂载时请求数据
onMounted(() => {
  fetchProducts();
});
</script>

<style scoped>
.product-container {
  display: flex;
  gap: 20px;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

/* 左侧分类栏 */
.category-nav {
  width: 200px;
  background: #f5f5f5;
  border-radius: 8px;
  padding: 20px;
}
.category-nav h3 {
  margin-bottom: 15px;
  color: #333;
}
.category-nav ul {
  list-style: none;
}
.category-nav li {
  padding: 12px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}
.category-nav li:hover {
  background: #e0e0e0;
}
.category-nav li.active {
  background: #2196f3;
  color: white;
}

/* 右侧商品列表 */
.product-list {
  flex: 1;
}
.product-list h2 {
  margin-bottom: 20px;
  color: #333;
}
.update-time {
  font-size: 0.8rem;
  color: #666;
  font-weight: normal;
  margin-left: 10px;
}

/* 加载状态 */
.loading {
  text-align: center;
  padding: 50px 0;
  color: #666;
}

/* 错误状态 */
.error {
  text-align: center;
  padding: 50px 0;
  color: #f44336;
}
.error button {
  margin-top: 10px;
  padding: 8px 16px;
  background: #2196f3;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/* 商品网格 */
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}
.product-item {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: transform 0.3s;
}
.product-item:hover {
  transform: translateY(-5px);
}
.product-img {
  height: 180px;
  background: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  color: #999;
}
.product-info {
  padding: 15px;
}
.product-info h3 {
  font-size: 1.1rem;
  margin-bottom: 8px;
  color: #333;
}
.product-info p {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 5px;
}
.price {
  color: #f44336;
  font-weight: bold;
  margin-top: 10px;
}

/* 空状态 */
.empty {
  grid-column: 1/-1;
  text-align: center;
  padding: 50px 0;
  color: #666;
}
</style>